﻿Imports Mystery

Public Class ReadOnlyReference
    Implements IPropertyControl


    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentReference


    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentReference)()
            _label = _content_property.label
            _property_info = value
        End Set
    End Property

    Private _value As ICollection(Of IContent) = New List(Of IContent)

    Public Property value As Object Implements IPropertyControl.value
        Get
            Return _value
        End Get
        Set(value As Object)
            If TypeOf (value) Is BaseContentReference Then value = DirectCast(value, BaseContentReference).getAsEnum()
            Dim new_value As IEnumerable(Of IContent) = value
            If new_value.sameContents(_value) Then Exit Property

            If new_value IsNot Nothing Then
                _value = New List(Of IContent)(new_value)
            Else
                _value = New List(Of IContent)()
            End If

            'interfance
            myPanel.Children.Clear()

            For Each new_content As IContent In _value
                If new_content Is Nothing Then Continue For

                Dim cb As New Button
                Dim icb As IContentButton = new_content.GetType.getMysteryAttribute(Of ContentButtonAttribute)().createButton(new_content)
                AddHandler cb.Click, AddressOf icb.Press
                cb.Content = icb.getText
                cb.MinWidth = 100
                Dim image As Image = icb.getImage
                image.Height = 30
                image.Width = 30

                Dim sp As New StackPanel With {.Orientation = Orientation.Horizontal}

                sp.Children.Add(image)
                sp.Children.Add(cb)
                myPanel.Children.Add(sp)

            Next
            RaiseEvent valueChanged()
        End Set
    End Property

    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property


    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return GridLength.Auto
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Dim pv As BaseContentReference = _content_property.retrive()(_content)
        pv.setWithEnum(_value)
        Return New List(Of IContent) From {_content}
    End Function

    Public Event valueChanged() Implements IPropertyControl.valueChanged
End Class
